package com.lbb.mall.product.service.impl;


import com.lbb.mall.product.entity.AttrEntity;
import com.lbb.mall.product.service.AttrService;
import com.lbb.mall.product.vo.AttrGroupWithattrVo;
import com.lbb.mall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbb.common.utils.PageUtils;
import com.lbb.common.utils.Query;

import com.lbb.mall.product.dao.AttrGroupDao;
import com.lbb.mall.product.entity.AttrGroupEntity;
import com.lbb.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        //if Key不为空
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj) ->
                        obj.eq("attr_group_id",key).or().like("attr_group_name",key)
                    ); //模糊查询
        }
        //if catelogId=0，查询全部
        if(catelogId == 0){
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            return new PageUtils(page);
        }else{//catelogId不为0，查询对应catelog
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page =
                    this.page(new Query<AttrGroupEntity>().getPage(params),
                            wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithattrVo> getAttrGroupWithattrByCatelogId(Long catelogId) {
        //1、查出当前分类下的所有分组
        List<AttrGroupEntity> entities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithattrVo> collect1 = entities.stream().map(item -> {
            AttrGroupWithattrVo attrGroupWithattrVo = new AttrGroupWithattrVo();
            BeanUtils.copyProperties(item, attrGroupWithattrVo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(item.getAttrGroupId());
            List<AttrVo> collect = attrEntities.stream().map(item2 -> {
                AttrVo attrVo = new AttrVo();
                BeanUtils.copyProperties(item2, attrVo);
                attrVo.setAttrGroupId(item.getAttrGroupId());
                return attrVo;
            }).collect(Collectors.toList());
            attrGroupWithattrVo.setAttrs(collect);
            return attrGroupWithattrVo;
        }).collect(Collectors.toList());
        return collect1;
    }


}